<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2017/3/21
  Time: 13:51
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    String wsPath = "ws://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
    <title>home</title>
</head>
<script type="text/javascript">
        //通过HTTP协议自动建立socket连接，服务端对"/socketServer"和"/sockjs/socketServer"进行拦截
        var sock;
        if ('WebSocket' in window) {
            sock = new WebSocket("<%=wsPath%>socketServer");
        } else if ('MozWebSocket' in window) {
            sock = new MozWebSocket("<%=wsPath%>socketServer");
        } else {
            sock = new SockJS("<%=basePath%>sockjs/socketServer");
        }

        sock.onopen = function (e) {
            console.log("websocket连接上")
            console.log(e);
        };
        sock.onmessage = function (e) {
            console.log(e)
            var ta = document.getElementById('message');
            ta.value = ta.value + '\n' + e.data;
        };
        sock.onerror = function (e) {
            console.log("websocket错误");
            console.log(e);
        };
        sock.onclose = function (e) {
            console.log("websocket关闭");
            console.log(e);
        }
</script>
<body>
<h1>
    Hello world!  This is a WebSocket demo!
</h1>
<h2>
    path:<%=path%><br/>
    basePath:<%=basePath%><br/>
    wsPath:<%=wsPath%><br/>
</h2>
<div>
    <textarea id="message" style="width: 500px; height: 300px;"></textarea>
</div>
</body>
</html>
